package com.shihang.crm.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.shihang.crm.bean.Customer;
import com.shihang.crm.bean.LinkMan;
import com.shihang.crm.service.LinkManService;
import com.shihang.crm.utils.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan> {
    LinkMan linkMan = new LinkMan();

    private LinkManService ls;
    //当前页数
    private int currentPage;
    //每页显示条数
    private int pageSize;

    public String add() throws Exception{
        ls.save(linkMan);
        return "toList";
    }


    public String list() throws Exception{
        //封装离线查询对象
        System.out.println("linkManlisttttttttttttttttttt");
        DetachedCriteria dc = DetachedCriteria.forClass(LinkMan.class);
        if(StringUtils.isNotBlank(linkMan.getLkm_name())){
            dc.add(Restrictions.like("lkm_name","%"+linkMan.getLkm_name()+"%"));
        }
        if(linkMan.getCustomer()!=null){
            dc.add(Restrictions.eq("customer.cust_id",linkMan.getCustomer().getCust_id()));
        }

        //1.调用Service查询分页数据（PageBean）
        PageBean pb = ls.getPageBean(dc,currentPage,pageSize);
        //2.将PageBean放入request域，转发到列表页面显示
        ActionContext.getContext().put("pageBean",pb);
        return "list";
    }

    @Override
    public LinkMan getModel() {
        return linkMan;
    }

    public void setLs(LinkManService ls) {
        this.ls = ls;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
}
